import { createSlice } from "@reduxjs/toolkit"
import axios from "axios"
const billStore = createSlice({
  name:'bill',
  initialState:{
    billList:[]
  },
  reducers:{
    setBillList(state,action) {
      state.billList = action.payload
    },
    // 添加新账单数据
    addBill(state,action) {
      state.billList.push(action.payload)
    }
  }
})
const {setBillList,addBill} = billStore.actions
// 编写异步请求代码，获取账单数据
const getBillListAsync = () => {
  return async(dispatch) => {
    const res = await axios.get('http://localhost:8888/ka')
    dispatch(setBillList(res.data))
  }
}
// 封装异步请求新增账单
const addBillList = (data) => {
  return async(dispatch) => {
    const res = await axios.post('http://localhost:8888/ka',data)
    dispatch(res.data)
  }
}
export {getBillListAsync,addBillList}
const reducer = billStore.reducer
export default reducer